package com.lingkang.usersso.service.impl;

import com.lingkang.usersso.entity.LogLogin;
import com.lingkang.usersso.entity.UsUser;
import com.lingkang.usersso.mapper.LogLoginMapper;
import com.lingkang.usersso.mapper.UsRoleMapper;
import com.lingkang.usersso.mapper.UsUserMapper;
import com.lingkang.usersso.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author linke
 * @date 2020-02-05 下午 15:45
 * @description
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UsUserMapper usUserMapper;
    @Autowired
    private LogLoginMapper logLoginMapper;

    @Autowired
    private UsRoleMapper usRoleMapper;

    @Override
    public UsUser getUserInfo(String username) {
        return usUserMapper.getUserInfo(username);
    }

    @Override
    public List<String> getRoleByUserId(Integer userId) {
        return usRoleMapper.getRoleByUserId(userId);
    }

    @Override
    public void addLoginLog(String username, String remarks, String ip) {
        LogLogin logLogin = new LogLogin();
        logLogin.setCreateTime(new Date());
        logLogin.setUsername(username);
        logLogin.setIp(ip);
        if (remarks != null)
            if (remarks.length() > 74)
                remarks = remarks.substring(0, 74);
        logLogin.setRemarks(remarks);
        logLoginMapper.insertSelective(logLogin);
    }

    @Override
    public Object list(int page, int size) {
        List<UsUser> users = usUserMapper.list((page - 1) * size, size);
        Map<String, Object> map = new HashMap<>();
        map.put("data", users);
        map.put("code", 0);
        map.put("count", usUserMapper.listCount(null));
        map.put("msg", null);
        return map;
    }
}
